package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.base.ResultMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.service.SlidePictureService;
import com.symone.webcms.model.SlidePicture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *   @description : 广告位图片 管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-03-29
 */
@RestController
@Api(value="/slidePicture", description="广告位图片 管理")
@RequestMapping("/api/slide/picture")
public class SlidePictureController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(SlidePictureController.class);

    @Autowired
    public SlidePictureService slidePictureService;

    /**
     * @description : 获取广告位图片分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-29
     */
    @RequiresPermissions("extend:slide:picture:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value="获取广告位图片分页列表", notes="获取广告位图片分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageSize",value="页大小",required=true,defaultValue = "10"),
            @ApiImplicitParam(name="pageNum",value="页数",required=true,defaultValue = "1")
    })
    public ResultMessage page(Integer pageSize,Integer pageNum) {
        Wrapper<SlidePicture> ew = new EntityWrapper<SlidePicture>();
        ew.eq("deleted",0).and().eq("lang",getSessionLanguage().getCode());

        //logger.debug("SQL:"+ew.originalSql());

        Page<SlidePicture> page = new Page<SlidePicture>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        page = slidePictureService.findPage(page, ew);

        return ResultMessage.success("获取广告位图片的分页列表",page.getRecords(),page.getTotal());
    }

    /**
     * @description : 获取所有广告位图片记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-29
     */
    @RequiresPermissions("extend:slide:picture:index")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有广告位图片信息", notes="获取所有广告位图片信息")
    public ResultMessage list() {
        return ResultMessage.success("获取所有广告位图片信息",slidePictureService.findAll());
    }

    /**
     * @description : 通过id获取广告位图片
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-29
     */
    @RequiresPermissions("extend:slide:picture:get")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value="获取广告位图片信息", notes="获取广告位图片信息")
    @ApiImplicitParam(name="id",value="广告位图片ID",required=true)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取广告位图片信息成功!",slidePictureService.selectById(id));
    }

    /**
     * @description : 添加广告位图片
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-29
     */
    @RequiresPermissions("extend:slide:picture:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value="添加广告位图片信息", notes="添加广告位图片信息")
    @ApiImplicitParam(name = "slidePicture", value = "广告位图片信息", required = true )
    public ResultMessage add(SlidePicture slidePicture){
        slidePicture.setCreateUser(getSessionUser().getUserName());
        slidePictureService.insert(slidePicture);
        return ResultMessage.success("添加广告位图片成功!");
    }

    /**
     * @description : 通过id逻辑删除广告位图片
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-29
     */
    @RequiresPermissions("extend:slide:picture:remove_flag")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    @ApiOperation(value="逻辑删除广告位图片", notes="逻辑删除广告位图片")
    @ApiImplicitParam(name = "id", value = "广告位图片Id", required = true )
    public ResultMessage removeFlag(Long id){
        if(slidePictureService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑广告位图片删除成功！");
        } else {
            return ResultMessage.fail("逻辑广告位图片删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除广告位图片
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-29
     */
    @RequiresPermissions("extend:slide:picture:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ApiOperation(value="物理删除广告位图片", notes="物理删除广告位图片")
    @ApiImplicitParam(name = "id", value = "广告位图片Id", required = true )
    public ResultMessage remove(long id){
        if(slidePictureService.deleteById(id)) {
            return ResultMessage.success("物理删除广告位图片成功！");
        } else {
            return ResultMessage.fail("物理删除广告位图片失败！");
        }
    }

    /**
     * @description : 更新广告位图片信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-29
     */
    @RequiresPermissions("extend:slide:picture:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value="更新广告位图片信息", notes="更新广告位图片信息")
    @ApiImplicitParam(name = "slidePicture", value = "广告位图片信息", required = true )
    public ResultMessage update(SlidePicture slidePicture) {
        if(slidePictureService.updateById(slidePicture)) {
            return ResultMessage.success("广告位图片信息更新成功!");
        }else{
            return ResultMessage.fail("广告位图片信息更新失败!");
        }
    }

    /**
    * @description : 清除广告位图片记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-29
    */
    @RequiresPermissions("extend:slide:picture:clear")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    @ApiOperation(value="清除广告位图片记录", notes="清除已被逻辑删除的广告位图片记录")
    public ResultMessage clear(){
        int num = slidePictureService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有广告位图片记录被清除！");
        }
    }



}
